Editing files with vim to increase the reason why content cannot be successful

Let's first understand some background knowledge:

1) Added i permission to the file, the file cannot be changed, cannot be deleted, and the name and permissions cannot be modified.

2) Add a permission to the file, the file can be added, cannot be deleted, the content cannot be modified, and the name and permissions cannot be modified.

3) vim a file, if it does not exit normally, it will prompt some information when editing again, and there is a hidden file.xxx.swp

After understanding the above knowledge, look at the following phenomenon:

1) If you add a permission to a file, edit the file with vim, add the content (note that the content is added at the end of the file, do not modify other content), and will not succeed.

2) If you add i permission or a permission to a directory, vim a file below the directory, and the contents of the changed file can be saved normally.

Since a permission can append content, why can't vim a file add content at the end? Since the i permission cannot be modified, why can the content of the file be changed in the directory?

Do you have any doubts about these two points? Let us analyze the reasons below.

Editing files with vim to increase the reason why content cannot be successful

Regardless of i or a permission,

Edit a file without i or a permissions in a directory without i or a permissions.

Use strace to see its execution.

Mkdir /tmp/test

Strace vim /tmp/test/aminglinux.txt 2>/tmp/vim.log

Write a number 1, then save and exit. Let's look at the contents of vim.log.

Less /tmp/vim.log

Most of the content you don't care about, just look at these lines:

Stat("/tmp/test/aminglinux.txt", 0x7fff072ecb10) = -1 ENOENT (No such file or directory)

Access("/tmp/test/aminglinux.txt", W_OK) = -1 ENOENT (No such file or directory)

Open("/tmp/test/aminglinux.txt", O_RDONLY) = -1 ENOENT (No such file or directory)

Readlink("/tmp/test/aminglinux.txt", 0x7fff072eb360, 4095) = -1 ENOENT (No such file or directory)

Open("/tmp/test/.aminglinux.txt.swp", O_RDONLY) = -1 ENOENT (No such file or directory)

Open("/tmp/test/.aminglinux.txt.swp", O_RDWR|O_CREAT|O_EXCL, 0600) = 3

Open("/tmp/test/.aminglinux.txt.swx", O_RDONLY) = -1 ENOENT (No such file or directory)

Open("/tmp/test/.aminglinux.txt.swx", O_RDWR|O_CREAT|O_EXCL, 0600) = 4

Unlink("/tmp/test/.aminglinux.txt.swx") = 0

Unlink("/tmp/test/.aminglinux.txt.swp") = 0

Stat("/tmp/test/.aminglinux.txt.swp", 0x7fff072ec310) = -1 ENOENT (No such file or directory)

Lstat("/tmp/test/.aminglinux.txt.swp", 0x7fff072ec3e0) = -1 ENOENT (No such file or directory)

Lstat("/tmp/test/.aminglinux.txt.swp", 0x7fff072ec8a0) = -1 ENOENT (No such file or directory)

Open("/tmp/test/.aminglinux.txt.swp", O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0600) = 3

Stat("/tmp/test/aminglinux.txt", 0x7fff072eac40) = -1 ENOENT (No such file or directory)

Stat("/tmp/test/aminglinux.txt", 0x7fff072ebe20) = -1 ENOENT (No such file or directory)

Stat("/tmp/test/aminglinux.txt", 0x7fff072eadf0) = -1 ENOENT (No such file or directory)

Write(1, ""/tmp/test/aminglinux.txt"", 26) = 26

Stat("/tmp/test/aminglinux.txt", 0x7fff072ec050) = -1 ENOENT (No such file or directory)

Open("/tmp/test/aminglinux.txt", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 4

Stat("/tmp/test/aminglinux.txt", {st_mode=S_IFREG|0644, st_size=2, ...}) = 0

Stat("/tmp/test/aminglinux.txt", {st_mode=S_IFREG|0644, st_size=2, ...}) = 0

Unlink("/tmp/test/.aminglinux.txt.swp") = 0

Looks chaotic, in fact, the approximate process is vim /tmp/test/aminglinux.txt, first look at whether there are .aminglinux.txt.swp and .aminglinux.txt.swx, because these two files are temporary generated by vim The file, swp is generated first, and if the swp exists, a second swx is generated. The written content is stored in swp first. When saving and exiting vim, save the contents of swp to aminglinux.txt, and finally delete the swp file.

With this understanding, let us analyze the phenomenon 1 mentioned above. If the file gives a permission, then when editing the file, a swp file will be generated. When the save is exited, the contents of the swp file will be written to the file, which is equivalent to changing the file, and the online a permission is not allowed.

Then analyze the phenomenon 2, according to our speculation, if the directory gives a permission, there is no problem to increase the file, that is to say, there is no problem with the swp or swx file. Of course, there is no problem when writing the contents of swp or swx to the file. However, the swp or swx file will not be deleted, so when you edit the file again, it will prompt that the temporary file already exists. But this does not affect the content of the modified file.

If the i permission is set for the directory, vim edits the file, and if you want to generate swp or swx, you will definitely get an error, but why can you still edit the file normally? Let's continue to analyze it with strace.

Chattr +i /tmp/test

Strace vim /tmp/test/aminglinux.txt 2> /tmp/vim.log

See the information related to aminglinux.txt in vim.log

Stat("/tmp/test/aminglinux.txt", {st_mode=S_IFREG|0644, st_size=4, ...}) = 0

Stat("/tmp/test/aminglinux.txt", {st_mode=S_IFREG|0644, st_size=4, ...}) = 0

Stat("/tmp/test/aminglinux.txt", {st_mode=S_IFREG|0644, st_size=4, ...}) = 0

Stat("/tmp/test/aminglinux.txt", {st_mode=S_IFREG|0644, st_size=4, ...}) = 0

Access("/tmp/test/aminglinux.txt", W_OK) = 0

Open("/tmp/test/aminglinux.txt", O_RDONLY) = 3

Readlink("/tmp/test/aminglinux.txt", 0x7fff49efc6f0, 4095) = -1 EINVAL (Invalid argument)

Open("/tmp/test/.aminglinux.txt.swp", O_RDONLY) = -1 ENOENT (No such file or directory)

Open("/tmp/test/.aminglinux.txt.swp", O_RDWR|O_CREAT|O_EXCL, 0600) = -1 EACCES (Permission denied)

Stat("/tmp/test/.aminglinux.txt.swp", 0x7fff49efd6a0) = -1 ENOENT (No such file or directory)

Lstat("/tmp/test/.aminglinux.txt.swp", 0x7fff49efd770) = -1 ENOENT (No such file or directory)

Lstat("/tmp/test/.aminglinux.txt.swp", 0x7fff49efdc30) = -1 ENOENT (No such file or directory)

Open("/tmp/test/.aminglinux.txt.swp", O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0600) = -1 EACCES (Permission denied)

Readlink("/tmp/test/aminglinux.txt", 0x7fff49efc6f0, 4095) = -1 EINVAL (Invalid argument)

Open("/root/tmp/aminglinux.txt.swp", O_RDONLY) = -1 ENOTDIR (Not a directory)

Open("/root/tmp/aminglinux.txt.swp", O_RDWR|O_CREAT|O_EXCL, 0600) = -1 ENOTDIR (Not a directory)

Stat("/root/tmp/aminglinux.txt.swp", 0x7fff49efd6a0) = -1 ENOTDIR (Not a directory)

Lstat("/root/tmp/aminglinux.txt.swp", 0x7fff49efd770) = -1 ENOTDIR (Not a directory)

Lstat("/root/tmp/aminglinux.txt.swp", 0x7fff49efdc30) = -1 ENOTDIR (Not a directory)

Open("/root/tmp/aminglinux.txt.swp", O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0600) = -1 ENOTDIR (Not a directory)

Readlink("/tmp/test/aminglinux.txt", 0x7fff49efc6f0, 4095) = -1 EINVAL (Invalid argument)

Open("/var/tmp/aminglinux.txt.swp", O_RDONLY) = -1 ENOENT (No such file or directory)

Open("/var/tmp/aminglinux.txt.swp", O_RDWR|O_CREAT|O_EXCL, 0600) = 4

Open("/var/tmp/aminglinux.txt.swx", O_RDONLY) = -1 ENOENT (No such file or directory)

Open("/var/tmp/aminglinux.txt.swx", O_RDWR|O_CREAT|O_EXCL, 0600) = 5

Unlink("/var/tmp/aminglinux.txt.swx") = 0

Unlink("/var/tmp/aminglinux.txt.swp") = 0

Stat("/var/tmp/aminglinux.txt.swp", 0x7fff49efd6a0) = -1 ENOENT (No such file or directory)

Lstat("/var/tmp/aminglinux.txt.swp", 0x7fff49efd770) = -1 ENOENT (No such file or directory)

Lstat("/var/tmp/aminglinux.txt.swp", 0x7fff49efdc30) = -1 ENOENT (No such file or directory)

Open("/var/tmp/aminglinux.txt.swp", O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0600) = 4

Chmod("/var/tmp/aminglinux.txt.swp", 0644) = 0

Open("/tmp/test/aminglinux.txt", O_RDONLY) = 3

Stat("/tmp/test/aminglinux.txt", {st_mode=S_IFREG|0644, st_size=4, ...}) = 0

Stat("/tmp/test/aminglinux.txt", {st_mode=S_IFREG|0644, st_size=4, ...}) = 0

Stat("/tmp/test/aminglinux.txt", {st_mode=S_IFREG|0644, st_size=4, ...}) = 0

Access("/tmp/test/aminglinux.txt", W_OK) = 0

Write(1, ""aminglinux.txt"", 16) = 16

Stat("aminglinux.txt", {st_mode=S_IFREG|0644, st_size=4, ...}) = 0

Access("aminglinux.txt", W_OK) = 0

Getxattr("aminglinux.txt", "system.posix_acl_access", 0x7fff49efd050, 132) = -1 ENODATA (No data available)

Stat("aminglinux.txt", {st_mode=S_IFREG|0644, st_size=4, ...}) = 0

Open("aminglinux.txt", O_WRONLY|O_CREAT|O_TRUNC, 0644) = 3

Chmod("aminglinux.txt", 0100644) = 0

Setxattr("aminglinux.txt", "system.posix_acl_access", "x02x00x00x00x01x00x06x00xffxffxffxffx04x00x04x00xffxffxffxff x00x04x00xffxffxffxff", 28, 0) = 0

Stat("/tmp/test/aminglinux.txt", {st_mode=S_IFREG|0644, st_size=6, ...}) = 0

Unlink("/var/tmp/aminglinux.txt.swp") = 0

I believe that you can see the Permission denied prompt, this is because the current directory has i permissions, you can not add files, you can not generate temporary files in the current directory. Of course, if vim encounters such a problem, it will still "curve save the country", so first find /root/tmp/, but there is no such directory, so I have to continue to find /var/tmp/, this directory exists, so it is Temporary files (not hidden) are generated in this directory. Needless to say later.

Since vim can generate temporary files under /var/tmp/, it is natural to edit the files in the directory where i permissions have been set. This phenomenon 2 is also explained. There are so many things in it, in fact, I want to express the following points:

When vim edits a file, it will generate temporary hidden files .swp and .swx in the directory where the file is located. If the directory is not writable, it will generate temporary files (not hidden) under /root/tmp/ or /var/tmp/. Temporary files are deleted when the edited file is saved.

Stainless Steel Industrial Pipe

ShenZhen Haofa Metal Precision Parts Technology Co., Ltd. , https://www.haofametals.com